
<script type="text/javascript">
var __time_out = 1000;//Shop.set.refer_timeout;
window.addEvent('domready',function(){

    var ReferObj =new Object();
    Object.append(ReferObj,{
        serverTime:<{$smarty.now}>,
        init:function(){
            var FIRST_REFER=Cookie.read('S[FIRST_REFER]');
            var NOW_REFER=Cookie.read('S[NOW_REFER]');
            var nowDate=this.time=this.serverTime*1000;
            if(!window.location.href.test('#r-')&&!document.referrer||document.referrer.test(document.domain))return;
            if(window.location.href.test('#r-'))Cookie.dispose('S[N]');
            if(!FIRST_REFER){

                if(NOW_REFER){
                    this.writeCookie('S[FIRST_REFER]',NOW_REFER,this.getTimeOut(JSON.decode(NOW_REFER).DATE));
                }else{
                    this.setRefer('S[FIRST_REFER]',__time_out);
                }
            }
            this.setRefer('S[NOW_REFER]',__time_out);
            this.createGUID();
        },
        getUid:function(){
            var lf=window.location.href,pos=lf.indexOf('#r-');
            return pos!=-1?lf.substr(pos+4):'';
        },
        getRefer:function(){
            return document.referrer?document.referrer:'';
        },
        setRefer:function(referName,timeout){
            var uid=this.getUid(),referrer=this.getRefer();
            var data={'ID':uid,'REFER':referrer,'DATE':this.time};

            if('S[NOW_REFER]'==referName){
                var refer=JSON.decode(Cookie.read('S[FIRST_REFER]'));
                if(uid!=''&&refer&&refer.ID==''){
                    var fdata={'ID':uid,'REFER':refer.REFER,'DATE':refer.DATE};
                    this.writeCookie('S[FIRST_REFER]',JSON.encode(fdata),this.getTimeOut(refer.DATE));
                }else if(uid==''){
                    Object.append(data,{'ID':refer.ID});
                }
            }
            Cookie.write(referName,JSON.encode(data),{duration:(__time_out||15)});
        },
        getTimeOut:function(nowDate){
            var timeout=nowDate+__time_out*24*3600*1000;
            var date=new Date(timeout);
            return date;
        },
        writeCookie:function(key,value,timeout){
            document.cookie=key+ '=' + value+'; expires=' + timeout.toGMTString();
        },
        createGUID:function(){
            var GUID = (function(){
                var S4=function(){
                    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
                };
                return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()).toUpperCase();
            })();
            Cookie.write('S[N]',GUID,{duration:3650});
        }
    });
    ReferObj.init();
});


  </script>

<script>
var Shop = <{$shopDefine}>;
</script>
<{if $openid_open == 'true'}>
<script>
(function(){
 RemoteLogin={
        init:function(){
             if(!$$('.trust__login')||$$('.trustdialog').length)return;
             $$('.trust__login').removeEvents('click').addEvent('click',this.show.bind(this));
        },
        show:function(){
             new Request({
                url:'<{link app="openid" ctl="site_trust" act="trust_login"}>',
                onRequest:function(){
                    var winWidth = 440,
                    winHeight = 330,
                    winTop=(screen.availHeight - winHeight)/2,
                    winLeft=(screen.availWidth - winWidth)/2,
                    param0 = "toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no, status=no,directories=no",
                    params = "top=" + winTop + ",left=" + winLeft + ",width=" + winWidth + ",height=" + winHeight + "," + param0;
                    this.opt = params;
                    //this.title = "信任登录";
                    /*
                      this.loginDialog = new Element('div',{'class':'dialog trustdialog','id':'trust_footer_login'}).set('html',document.getElement('#template-modal .dialog').get('html').substitute({
                      title:'信任登录',
                      content:'<iframe src="" id="RemoteFrm" width="100%" height="90%" frameborder="0" styles="border:none;background: none repeat scroll 0% 0% transparent; "></iframe>'
                       })).setStyles({display:'none',width:0}).inject(document.body);

                      this.loginDialog.getElement('.dialog-close-btn').addEvent('click',function(){$('trust_footer_login').destroy();});
                      this.loginDialog.setStyles({width:440,height:330,display:'block'}).amongTo(window);
                  */
                  },
                onComplete:function(e){
                     if(e){
                          var remotesrc='http://openid.ecos.shopex.cn/?certi_id=<{$cert_id}>&callback_url=<{link app="openid" ctl="site_trust" act="other_login" full="1"}>&refertype=ecstore';
                      }else{
                         var remotesrc='<{link app="openid" ctl="site_trust" act="error"}>';
                      }
                     //this.loginDialog.getElement('iframe').src=remotesrc;
                      //location.href = remotesrc;
                      var NewWin = open(remotesrc,"信任登录",this.opt);
                      //NewWin.document.title = "信任登录";
                }
            }).post();
        }
    };
    RemoteLogin.init();
})();
</script>
<{/if}>

<script>
/*
迷你购物车
@author litie[aita]shopex.cn
  [c] shopex.cn
*/

 window.addEvent('domready',function(){
      miniCart={
           'show':function(target){
               target = $(target);
               if (!target) return;
               var _this=this, dialog  = this.dialog =$('mini-cart-dialog') || new Element('div#mini-cart-dialog.popup-container.mini-cart-dialog',{
                  'html': $('popup-template').get('html').substitute({
                      body: 'popup-body',
                      header: 'popup-header',
                      close: 'popup-btn-close',
                      content:'popup-content',
                      title:'<{t}>正在加入购物车<{/t}>',
                      main:'<div class="loading">loading...</div>'
                  }),
                  styles:{
                      display:'block'
                  }
               }).inject(document.body);
               var setSize = this.setSize = function(){
                  $(_this.dialog).setStyles({
                      top:target.getPosition().y+target.getSize().y,
                      left:target.getPosition().x.limit(0,window.getSize().x-_this.dialog.getSize().x) + window.getScroll().x
                  });
               }
               setSize();

               document.addEvent('click',function(e){
                  _this.hide.delay(100,_this);
                  document.removeEvent('click',arguments.callee);
               });
               window.addEvent('resize',setSize);

           },
           'hide':function(){
               try{
                 this.dialog.destroy();
                 window.removeEvent('resize',this.setSize);
               }catch(e){}
           },
           'load':function(){
              var params=Array.flatten(arguments).link({
                  'remoteURL':Type.isString,
                  'options':Type.isObject
              });
              params.options.data = params.options.data?params.options.data.toQueryString()+'&mini_cart=true':'&mini_cart=true';
              var opts=params=Object.append({
                 url:params.remoteURL,
                 method:'post',
                 onRequest:function(){
                     //this.dialog.getElement('.title').set('html','正在加入购物车');
                 }.bind(this),
                 onSuccess:function(re){
                     var _json_re;
                     try{
                        _json_re = JSON.decode(re);
                     }catch(e){}

                     if( _json_re && _json_re.url  ) {
                        location.href = _json_re.url;
                     } else if( _json_re && _json_re.error  ) {
                        $(this.dialog).destroy();
                        MessageBox.error( _json_re.error );
                     } else {
                         if($$('.cart-number')) $$('.cart-number').set('text',Cookie.read('S[CART_NUMBER]')||0);
                         if($$('.cart-count')) $$('.cart-count').set('text',Cookie.read('S[CART_COUNT]')||0);
                         if($$('.cart-money-total')) $$('.cart-money-total').set('text',Cookie.read('S[CART_TOTAL_PRICE]')||0);
                         this.dialog.getElement('h2').set('html','<{t}>成功加入购物车<{/t}>');
                         this.dialog.getElement('.popup-content').innerHTML=re;

                         if($('MyCart')){//todo
                            $('MyCart').retrieve('fn:refresh',function(){})();
                         }

                         if(document.getElement('.minicart_box')&&document.getElement('.minicart_box').get('show_gallery')){
                            document.getElement('.minicart_box').fireEvent('_show');  //迷你购物车
                         }
                     }
                 }.bind(this),
                 onFailure:function(xhr){
                     this.dialog.destroy();
                     MessageBox.error("<{t}>加入购物车失败.<{/t}><br /><ul><li><{t}>可能库存不足.<{/t}></li><li><{t}>或提交信息不完整.<{/t}></li></ul>");
                 }.bind(this)
              },params.options||{});
              if(!params.url)return false;

              miniCart.show(opts.target);
              new Request(opts).send();
           },
           init:function(){
               var linktocart=$$('a[target=_dialog_minicart]');
               if(linktocart.length){
                    linktocart.addEvent('click',function(e){
                         e.stop();
                         if(!this.hasClass('hasSpec'))
                            miniCart.load([{url:this.href,data:this.getParent('tr'),target:this}]);
                    });
               }
           }
     };

     miniCart.init();

   var formtocart,btnbuy;

   if(formtocart=document.getElement('form[target=_dialog_minicart]')){
       formtocart.addEvent('submit',function(e){
           e.stop();
           this.getElement('[type=submit]').removeClass('disabled');
           miniCart.load([{
               url:this.action,
               method:this.method,
               data:this,
               target:this.getElement('input[value=<{t}>加入购物车<{/t}>]')
           }]);
       });
   };
   /*for  goods which has specs*/
   if($('goods-viewer') && $('goods-viewer').getElement('form[target=_dialog_minicart] .btn-buy')){

      $('goods-viewer').getElement('form[target=_dialog_minicart] .btn-buy').removeEvents('click').addEvent('click',function(e){
          e.stop();
          if(this.retrieve('tip:text'))return false;
          this.blur();
          this.form.fireEvent('submit',e);

      });

   };

});

window.addEvent('domready',function(){
    specdialog =function(contain){
        contain=contain||document;
        var hasSpec=contain.getElements('.hasSpec'), url='<{$request_url}>';
        if(!hasSpec)return;

        hasSpec.each(function(specBtn,i){
            var item=specBtn.getParent('.itemsList')||contain.getElements('.itemsList')[i];
            if(!item)return;
            item.addEvent('showSpec',function(e){
                 e.stop();
                 var _this= this;
                 if(this.retrieve('showSpec') && !document.getElement('.dialog-specbox')) return;
                 //document.getElement('.dialog-specbox') && document.getElement('.dialog-specbox').getParent('[data-single]').retrieve('instance').hide();
                 url=(url && !!url.trim().length)?url:this.get('url');
                 var dialog = new Ex_Dialog(url,{
                    onLoad:function(){
                      if(document.getElement('.dialog-specbox')) {
                        this.content.set('html',this.options.ajaxTpl);
                        try{miniCart.hide();}catch(e){}
                      }
                    },
                    async:'ajax',
                    asyncOptions:{
                      data:'gid='+this.get('product'),
                      onSuccess:function(){
                        //try{this.body.getElement('.arrowleft').destroy();}catch(e){}
                        var _form=this.body.getElement('form');
                        _form.addEvent('submit',function(e){
                            if(this.target!='_dialog_minicart') return this.submit();
                                miniCart.load([{
                                   url:this.action,
                                   method:this.method,
                                   data:this,
                                   target:this.getElement('.updateBtn')
                               }]);
                         });
                      }
                    },
                    single:true,
                    position:{target:this},
                    effect:{duration:250},
                    onClose:function(){item.eliminate('showSpec');try{miniCart.hide();}catch(e){}}
                });
                this.store('showSpec', true);
            });
            specBtn.addEvent('click',function(e){item.fireEvent('showSpec',e);});
        });
    }
    specdialog();
});
</script>

<script id="dropmenu-template" type="text/template">
  <div class="{body}">
    <div class="{content} clearfix">{main}</div>
  </div>
</script>
<script id="popup-template" type="text/template">
  <div class="{body}">
     <div class="{header} clearfix">
        <h2>{title}</h2>
        <span><button type="button" title="关闭" class="{close}" hidefocus>x</button></span>
    </div>
    <div class="{content} clearfix">{main}</div>
  </div>
</script>
